<?php
// 前台用户控制器
class UsersAction extends CommonAction {


	public function _initialize()
	{
    	$this->currentModel = D(MODULE_NAME);		
	}

    public function index()
    {
    	echo $_SESSION[C('USER_AUTH_FRONT_KEY')];
    }

    public function login()
    {
    	if($this->isPost())
    	{
    		$username=$this->_post('username');
    		$pwd=$this->_post('password');
    		$userInfo=$this->currentModel->findUserByLogin($username,$pwd);
    		if(!empty($userInfo))
    		{
    			$_SESSION[C('USER_AUTH_FRONT_KEY')]=$userInfo['id'];
    			$this->success('登录成功',U('Index/index'));
    		}else
    			{
    				$this->error('账号不存在或是密码不正确');
    			}
    	}else
    		{
    			if($_SESSION[C('USER_AUTH_FRONT_KEY')]>0)
                {
                    $this->success('已经登录',U('Index/index'));   
                }else
                    {
                       $this->display(); 
                    }
    				
    		}
    }

    public function reg()
    {
        if($this->isPost())
        {
            if($this->currentModel->create()===FALSE)
            {
                $this->error($this->currentModel->getError());
            }else
                {
                    if($this->currentModel->add()!==FALSE)
                    {
                        $_SESSION[C('USER_AUTH_FRONT_KEY')]=$this->currentModel->getLastInsID();
                        $this->success('成功',U(MODULE_NAME.'/index'));
                    }else
                        {
                            $this->error('失败');
                        }
                }
        }else
            {
                $this->display();
            }
    }
}